package com.coldworks.coldjoke.task;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.base.manager.BaseNetworkManager;
import com.coldworks.coldjoke.model.ReplyModel;
import com.coldworks.coldjoke.ui.JokePageCell;
import com.coldworks.coldjoke.util.CONST;
import com.coldworks.coldjoke.util.JsonUtil;
import com.coldworks.coldjoke.util.NetworkUtil;

public class GetJokeReplyTask extends AsyncTask<Void, Void, List<ReplyModel>> {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private Context context;
	private JokePageCell jokePageCell;
	private String jokeId;

	public GetJokeReplyTask(Context context, JokePageCell jokePageCell) {
		this.context = context;
		this.jokePageCell = jokePageCell;
		this.jokeId = jokePageCell.getJokeModel().getJokeId();
	}

	@Override
	protected List<ReplyModel> doInBackground(Void... params) {
		String url = CONST.URL.COMENT + "?action=getOneJokeComments&joke_id="
				+ jokeId + "&data_type=json";
		DefaultHttpClient httpclient;
		HttpGet get = new HttpGet(url);
		httpclient = BaseNetworkManager.getInstance().createHttpClient(context);
		JSONArray jsonArray = null;
		try {
			// String sessionId = UserManager.getInstance().getSessionId();
			// get.setHeader("COOKIE", "webpy_session_id=" + sessionId);
			HttpResponse response = httpclient.execute(get);
			if (isCancelled()) {
				log.info("cancelled");
				return null;
			}
			if (response.getStatusLine().getStatusCode() == 200) {
				Header contentEncoding = response
						.getFirstHeader("Content-Encoding");
				JSONObject jsonObject = null;
				if (contentEncoding != null
						&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
					jsonObject = NetworkUtil.getInstance().getJsonObjectByGzip(
							response.getEntity().getContent());
				} else {
					jsonObject = NetworkUtil.getInstance().getJSONObject(
							response.getEntity().getContent());
				}
				if (jsonObject == null) {
					return null;
				}
				jsonArray = jsonObject.getJSONArray("comments");
				List<ReplyModel> list = JsonUtil.getReplyInfoList(jsonArray);
				return list;
			} else {
				log.info(String.valueOf(response.getStatusLine()
						.getStatusCode()));
				return null;
			}
		} catch (Exception e) {
			log.error("", e);
			return null;
		}
	}

	@Override
	protected void onPostExecute(List<ReplyModel> list) {
		super.onPostExecute(list);
		if (list != null) {
			if (this.jokeId.equals(jokePageCell.getJokeModel().getJokeId())) {
				jokePageCell.getJokeModel().setReplyInfoList(list);
				jokePageCell.refreshReply();
				log.info("get reply : " + list.size());
			} else {
				log.info("cell changed");
			}
		} else {
			log.info("get reply err");
		}
	}
}